package cn.jtcoding.common.framework.vo;

import cn.jtcoding.common.framework.enums.ApiResultCode;
import lombok.*;
import org.springframework.http.HttpStatus;

@Data
@Builder
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class ApiResult<T> {
	
	private Integer status = HttpStatus.OK.value();
	private String code;
	private String message;
	private T data;

	private void setResult(ApiResultCode resultCode) {
		this.code = resultCode.getCode();
		this.message = resultCode.getMsg();
	}
	
	/* 请求成功，没有实体信息返回，只返回成功状态  */
	public static <T> ApiResult<T> success() {
		ApiResult<T> apiResult = new ApiResult<T>();
		apiResult.setResult(ApiResultCode.SUCCESS);
		return apiResult;
	}
	
	/* 请求成功，返回实体信息 */ 
	public static <T> ApiResult<T> success(T data) {
		ApiResult<T> apiResult = new ApiResult<T>();
		apiResult.setResult(ApiResultCode.SUCCESS);
		apiResult.setData(data);
		return apiResult;
	}
	
	/* 出现错误，只返回错误信息 */ 
	public static <T> ApiResult<T> failure(ApiResultCode resultCode) {
		ApiResult<T> apiResult = new ApiResult<T>();
		apiResult.setResult(resultCode);
		return apiResult;
	}
	
	/* 出现错误，返回错误信息和实体信息 */ 
	public static <T> ApiResult<T> failure(ApiResultCode resultCode, T data) {
		ApiResult<T> apiResult = new ApiResult<T>();
		apiResult.setResult(resultCode);
		apiResult.setData(data);
		return apiResult;
	}
}
